package cn.wenhaha.app.data.controller;

import cn.wenhaha.app.data.bean.dto.DBConfigDTO;
import cn.wenhaha.app.data.bean.dto.QueryParam;
import cn.wenhaha.app.data.service.DataMysqlService;
import cn.wenhaha.common.core.domain.AjaxV1Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 数据源 <br/>
 * --------
 *
 * @author ：wyndem
 * @Date ：Created in 2022-04-03 19:06
 */
@RestController
@RequestMapping("/api/v1/data/mysql")
public class ApiDataMySqlController {

    @Autowired
    private DataMysqlService dataSourceService;


    @GetMapping("list")
    public AjaxV1Result list(QueryParam queryParam){
        return AjaxV1Result.newSuccess(dataSourceService.list(queryParam));
    }

    /**
     * <p>
     *    保存数据库信息
     * </p>
     * @Author: Wyndem
     * @DateTime: 2022-04-04 12:22
     */
    @PostMapping("save")
    public AjaxV1Result save(@RequestBody @Validated DBConfigDTO dbConfigDTO){
        return AjaxV1Result.newSuccess(dataSourceService.save(dbConfigDTO));
    }


    /**
     * <p>
     *    通过id来测试tokn
     * </p>
     * @Author: Wyndem
     * @DateTime: 2022-04-13 11:40
     */
    @GetMapping("test/{id}")
    public AjaxV1Result<String> test(@PathVariable("id") Integer id){
        dataSourceService.test(id);
        return AjaxV1Result.newSuccess("当前数据库可用");
    }


    /**
     * <p>
     *    通过id查询详情
     * </p>
     * @Author: Wyndem
     * @DateTime: 2022-04-14 9:55
     */
    @GetMapping("{id}")
    public AjaxV1Result info(@PathVariable("id") Integer id){
        return AjaxV1Result.newSuccess(dataSourceService.info(id));
    }


    /**
     * <p>
     *    删除
     * </p>
     * @Author: Wyndem
     * @DateTime: 2022-04-14 9:58
     */
    @GetMapping("delete/{id}")
    public  AjaxV1Result delete(@PathVariable("id") Integer id){
        dataSourceService.delete(id);
        return AjaxV1Result.newSuccess();
    }

}
